package cn.xwlin.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpUtils {

    public static String getMatchUser(String userId, String token, String headerJson) {
        String url = "https://fccdn1.k4n.cc/fc/wx_api/v1/Member/getCrewsList?lid2=" + userId;
        HttpPost httpPost = new HttpPost(url);
        JSONObject jsonObject;
        try {
            jsonObject = JSONObject.parseObject(headerJson);
            if (jsonObject == null) {
                jsonObject = new JSONObject();
            }
        } catch (Exception e) {
            jsonObject = new JSONObject();
        }

        httpPost.setHeader("Authorization", "Bearer " + token);
        if (jsonObject.containsKey("Cookie")) {
            httpPost.setHeader("Cookie", jsonObject.getString("Cookie"));
        }
        if (jsonObject.containsKey("Cookie")) {
            httpPost.setHeader("Cookie", jsonObject.getString("Cookie"));
        }
        if (jsonObject.containsKey("Cache-Control")) {
            httpPost.setHeader("Cache-Control", jsonObject.getString("Cache-Control"));
        } else {
            httpPost.setHeader("Cache-Control", "no-cache");
        }
        if (jsonObject.containsKey("Content-Type")) {
            httpPost.setHeader("Content-Type", jsonObject.getString("Content-Type"));
        } else {
            httpPost.setHeader("Content-Type", "application/json");
        }
        if (jsonObject.containsKey("Accept")) {
            httpPost.setHeader("Accept", jsonObject.getString("Accept"));
        } else {
            httpPost.setHeader("Accept", "*/*");
        }
        if (jsonObject.containsKey("Accept-Encoding")) {
            httpPost.setHeader("Accept-Encoding", jsonObject.getString("Accept-Encoding"));
        } else {
            httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
        }
        if (jsonObject.containsKey("Connection")) {
            httpPost.setHeader("Connection", jsonObject.getString("Connection"));
        } else {
            httpPost.setHeader("Connection", "keep-alive");
        }
        if (jsonObject.containsKey("Referer")) {
            httpPost.setHeader("Referer", jsonObject.getString("Referer"));
        } else {
            httpPost.setHeader("Referer", "https://servicewechat.com/wxffa42ecd6c0e693d/78/page-frame.html");
        }
        httpPost.setHeader("Host", "fccdn1.k4n.cc");
        if (jsonObject.containsKey("User-Agent")) {
            httpPost.setHeader("User-Agent", jsonObject.getString("User-Agent"));
        } else {
            httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090a13) UnifiedPCWindowsWechat(0xf2541411) XWEB/16965");
        }

        return doPost(httpPost, "{}");
    }

    public static void sendNotice(String userId) {
        try {
            String feishuUrl = "https://open.feishu.cn/open-apis/bot/v2/hook/0641c799-b850-4c9e-8cee-bd4837ab82f2";
            JSONObject feishuContentJson = new JSONObject();
            feishuContentJson.put("text", "<at user_id=\"all\">所有人</at>Get Ticket-Java Code-No JiaMi:" + userId);
            JSONObject feishuJson = new JSONObject();
            feishuJson.put("msg_type", "text");
            feishuJson.put("content", feishuContentJson);
            HttpPost httpPost = new HttpPost(feishuUrl);
            doPost(httpPost, JSON.toJSONString(feishuJson));
        } catch (Exception e) {
            System.out.println("###发送通知失败");
        }
    }

    public static String createMatchOrder(String userId, String loginCode, String token, String requestBody, String headerJson) {
        // 获取链接
        String key = "Y6xkC7b5";
        String time = String.valueOf(System.currentTimeMillis() / 1000);
        String signatureString = key + time + loginCode + userId;
        String result = md5(signatureString);
        String url = "https://fccdn5.k4n.cc/fc/wx_api/v1/MatchCon/createMatchOrder?lid2=" + userId + "&s_time=" + time + "&s_sign=" + result;

        HttpPost httpPost = new HttpPost(url);
        JSONObject jsonObject;
        try {
            jsonObject = JSONObject.parseObject(headerJson);
            if (jsonObject == null) {
                jsonObject = new JSONObject();
            }
        } catch (Exception e) {
            jsonObject = new JSONObject();
        }

        httpPost.setHeader("Authorization", "Bearer " + token);
        if (jsonObject.containsKey("Cookie")) {
            httpPost.setHeader("Cookie", jsonObject.getString("Cookie"));
        }
        if (jsonObject.containsKey("Cache-Control")) {
            httpPost.setHeader("Cache-Control", jsonObject.getString("Cache-Control"));
        } else {
            httpPost.setHeader("Cache-Control", "no-cache");
        }
        if (jsonObject.containsKey("Content-Type")) {
            httpPost.setHeader("Content-Type", jsonObject.getString("Content-Type"));
        } else {
            httpPost.setHeader("Content-Type", "application/json");
        }
        if (jsonObject.containsKey("Accept")) {
            httpPost.setHeader("Accept", jsonObject.getString("Accept"));
        } else {
            httpPost.setHeader("Accept", "*/*");
        }
        if (jsonObject.containsKey("Accept-Encoding")) {
            httpPost.setHeader("Accept-Encoding", jsonObject.getString("Accept-Encoding"));
        } else {
            httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
        }
        if (jsonObject.containsKey("Connection")) {
            httpPost.setHeader("Connection", jsonObject.getString("Connection"));
        } else {
            httpPost.setHeader("Connection", "keep-alive");
        }
        if (jsonObject.containsKey("Referer")) {
            httpPost.setHeader("Referer", jsonObject.getString("Referer"));
        } else {
            httpPost.setHeader("Referer", "https://servicewechat.com/wxffa42ecd6c0e693d/78/page-frame.html");
        }
        httpPost.setHeader("Host", "fccdn5.k4n.cc");
        if (jsonObject.containsKey("User-Agent")) {
            httpPost.setHeader("User-Agent", jsonObject.getString("User-Agent"));
        } else {
            httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090a13) UnifiedPCWindowsWechat(0xf2541411) XWEB/16965");
        }
        return doPost(httpPost, requestBody);
    }

    private static String getCreateMatchOrderUrl(String loginCode, String userId) {
        String key = "Y6xkC7b5";
        String time = String.valueOf(System.currentTimeMillis() / 1000);
        String signatureString = key + time + loginCode + userId;
        String result = md5(signatureString);
        String url = "https://fccdn5.k4n.cc/fc/wx_api/v1/MatchCon/createMatchOrder?lid2=" + userId + "&s_time=" + time + "&s_sign=" + result;
        return url;
    }


    private static String md5(String input) {
        try {
            java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes("UTF-8"));
            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (Exception e) {
            throw new RuntimeException("MD5计算失败", e);
        }
    }


    private static String doPost(HttpPost httpPost, String requestBody) {
        // 下单
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            // body
            StringEntity entity = new StringEntity(requestBody);
            httpPost.setEntity(entity);
            try (CloseableHttpResponse response = client.execute(httpPost)) {
                if (response.getStatusLine().getStatusCode() == 200) {
                    HttpEntity responseEntity = response.getEntity();
                    if (responseEntity != null) {
                        String responStr = EntityUtils.toString(responseEntity);
                        return responStr;
                    }
                } else {
                    System.out.println("失败:" + response.getStatusLine().getStatusCode());
                }
            }
        } catch (Exception e) {
            System.out.println("异常");
        }
        return null;
    }


    private static HttpPost buildMatchUserWithHeader(String url, String token) {
        HttpPost httpPost = new HttpPost(url);
        JSONObject jsonObject = new JSONObject();

        httpPost.setHeader("Authorization", "Bearer " + token);
        if (jsonObject.containsKey("Cookie")) {
            httpPost.setHeader("Cookie", jsonObject.getString("Cookie"));
        }
        if (jsonObject.containsKey("Cookie")) {
            httpPost.setHeader("Cookie", jsonObject.getString("Cookie"));
        }
        if (jsonObject.containsKey("Cache-Control")) {
            httpPost.setHeader("Cache-Control", jsonObject.getString("Cache-Control"));
        } else {
            httpPost.setHeader("Cache-Control", "no-cache");
        }
        if (jsonObject.containsKey("Content-Type")) {
            httpPost.setHeader("Content-Type", jsonObject.getString("Content-Type"));
        } else {
            httpPost.setHeader("Content-Type", "application/json");
        }
        if (jsonObject.containsKey("Accept")) {
            httpPost.setHeader("Accept", jsonObject.getString("Accept"));
        } else {
            httpPost.setHeader("Accept", "*/*");
        }
        if (jsonObject.containsKey("Accept-Encoding")) {
            httpPost.setHeader("Accept-Encoding", jsonObject.getString("Accept-Encoding"));
        } else {
            httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
        }
        if (jsonObject.containsKey("Connection")) {
            httpPost.setHeader("Connection", jsonObject.getString("Connection"));
        } else {
            httpPost.setHeader("Connection", "keep-alive");
        }
        if (jsonObject.containsKey("Referer")) {
            httpPost.setHeader("Referer", jsonObject.getString("Referer"));
        } else {
            httpPost.setHeader("Referer", "https://servicewechat.com/wxffa42ecd6c0e693d/78/page-frame.html");
        }
        httpPost.setHeader("Host", "fccdn1.k4n.cc");
        if (jsonObject.containsKey("User-Agent")) {
            httpPost.setHeader("User-Agent", jsonObject.getString("User-Agent"));
        } else {
            httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090a13) UnifiedPCWindowsWechat(0xf2541411) XWEB/16965");
        }
        return httpPost;
    }

    private static HttpPost buildCreateMatchOrderWithHeader(String url, String token) {
        HttpPost httpPost = new HttpPost(url);
        JSONObject jsonObject = new JSONObject();

        httpPost.setHeader("Authorization", "Bearer " + token);
        if (jsonObject.containsKey("Cookie")) {
            httpPost.setHeader("Cookie", jsonObject.getString("Cookie"));
        }
        if (jsonObject.containsKey("Cache-Control")) {
            httpPost.setHeader("Cache-Control", jsonObject.getString("Cache-Control"));
        } else {
            httpPost.setHeader("Cache-Control", "no-cache");
        }
        if (jsonObject.containsKey("Content-Type")) {
            httpPost.setHeader("Content-Type", jsonObject.getString("Content-Type"));
        } else {
            httpPost.setHeader("Content-Type", "application/json");
        }
        if (jsonObject.containsKey("Accept")) {
            httpPost.setHeader("Accept", jsonObject.getString("Accept"));
        } else {
            httpPost.setHeader("Accept", "*/*");
        }
        if (jsonObject.containsKey("Accept-Encoding")) {
            httpPost.setHeader("Accept-Encoding", jsonObject.getString("Accept-Encoding"));
        } else {
            httpPost.setHeader("Accept-Encoding", "gzip, deflate, br");
        }
        if (jsonObject.containsKey("Connection")) {
            httpPost.setHeader("Connection", jsonObject.getString("Connection"));
        } else {
            httpPost.setHeader("Connection", "keep-alive");
        }
        if (jsonObject.containsKey("Referer")) {
            httpPost.setHeader("Referer", jsonObject.getString("Referer"));
        } else {
            httpPost.setHeader("Referer", "https://servicewechat.com/wxffa42ecd6c0e693d/78/page-frame.html");
        }
        httpPost.setHeader("Host", "fccdn5.k4n.cc");
        if (jsonObject.containsKey("User-Agent")) {
            httpPost.setHeader("User-Agent", jsonObject.getString("User-Agent"));
        } else {
            httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090a13) UnifiedPCWindowsWechat(0xf2541411) XWEB/16965");
        }
        return httpPost;
    }

}
